//
//  FQDArticleFeedBaseViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/17/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDArticleFeedBaseViewController.h"
#import "FQDArticleDetailViewController.h"
#import "FQDLabOpinionViewController.h"
#import "FQDLabVoteDetailViewController.h"
#import "FQDFeedCellItemProvider.h"
#import "FQDRestModel.h"
#import "FQDNavigationView.h"
#import "FQDHomeFeedCell.h"
#import "UIViewController+FQDStatusBar.h"
#import "FQDArticleFeedCellItem.h"
#import "FQDLabFeedCellItem.h"

#import "Masonry.h"

@interface FQDArticleFeedBaseViewController ()

@end

@implementation FQDArticleFeedBaseViewController

#pragma mark View life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fqd_statusBarStyle = FQDStatusBarStyleLightTranslucent;
    [self setupNavigationView];
    [self setupFeedContentView];
    [self loadNewCellItemsWithCompletion:nil];
}

- (void)setupNavigationView
{
    CGRect navFrame = CGRectMake(0, 0, 320.0f, 64.0f);
    self.navView = [[FQDNavigationView alloc] initWithFrame:navFrame];
    self.navView.showBlurBg = YES;
    [self.view addSubview:self.navView];
    
    self.navView.titleLabel.text = self.title;
    [self.navView.backButton addTarget:self action:@selector(popBack:) forControlEvents:UIControlEventTouchUpInside];
    
    WeakSelf;
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@64);
    }];
}

- (void)setupFeedContentView
{
    WeakSelf;
    [self.feedContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.navView.mas_bottom);
        make.left.right.and.bottom.equalTo(weakSelf.view);
    }];
}

- (void)setHidesNavigationView:(BOOL)hidesNavigationView
{
    if (_hidesNavigationView != hidesNavigationView) {
        if (self.navView) {
            CGFloat height = (hidesNavigationView) ? 0 : 64;
            [self.navView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(height));
            }];
            self.navView.hidden = (hidesNavigationView);
        }
    }
    
    _hidesNavigationView = hidesNavigationView;
}

#pragma mark - FQDFeedBaseViewController override
- (NSInteger)numberOfHeaderCellsInCollectionView:(UICollectionView *)collectionView
{
    return 0;
}

- (Class)cellClassForCellItem:(FQDFeedCellItem *)cellItem
{
    return [cellItem cellClass];
}

- (NSArray *)cellClasses
{
    return @[
        [FQDRegularArticleFeedCell class],
        [FQDLargeArticleFeedCell class],
        [FQDLabFeedCell class],
        [FQDColumnFeedCell class],
        [FQDRegularArticleADFeedCell class],
    ];
}

#pragma mark - Event response
- (void)popBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)presentCellItem:(FQDFeedCellItem *)cellItem atIndexPath:(NSIndexPath *)indexPath
{
    if ([cellItem isKindOfClass:[FQDArticleFeedCellItem class]]) {
        [self presentArticleCellItem:cellItem atIndexPath:indexPath];
    } else if ([cellItem isKindOfClass:[FQDLabFeedCellItem class]]) {
        [self presentLabCellItem:cellItem atIndexPath:indexPath];
    }
}

- (void)presentArticleCellItem:(FQDFeedCellItem *)cellItem atIndexPath:(NSIndexPath *)indexPath
{
    FQDFeed *castFeed = (FQDFeed *)cellItem.restModel;
    FQDArticleDetailViewController *articleDetailVC = [[FQDArticleDetailViewController alloc] init];
    articleDetailVC.post = castFeed.post;
    [self.navigationController pushViewController:articleDetailVC animated:YES];
}

- (void)presentLabCellItem:(FQDFeedCellItem *)cellItem atIndexPath:(NSIndexPath *)indexPath
{
    FQDLabFeedCellItem *labFeedCellItem = (FQDLabFeedCellItem *)cellItem;
    FQDFeed *labFeed = labFeedCellItem.feed;
    if (labFeed.post.genre == FQDPostGenreOpinion) {
        FQDLabOpinionViewController *loVC = [[FQDLabOpinionViewController alloc] init];
        loVC.labFeedCellItem = labFeedCellItem;
        [self.navigationController pushViewController:loVC animated:YES];
    } else if (labFeed.post.genre == FQDPostGenreVote) {
        FQDLabVoteDetailViewController *lvdVC = [[FQDLabVoteDetailViewController alloc] init];
        lvdVC.labFeedCellItem = labFeedCellItem;
        [self.navigationController pushViewController:lvdVC animated:YES];
    }
}

- (void)didSelectCellItem:(FQDFeedCellItem *)cellItem atIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    if (self.cellSelectionBlock) {
        self.cellSelectionBlock(collectionView, indexPath, cellItem);
    } else {
        [self presentCellItem:cellItem atIndexPath:indexPath];
    }
}

@end